//
//  ZhangDanListTableViewCell.m
//  ErJie
//
//  Created by yaorenjin on 2017/1/31.
//  Copyright © 2017年 yaorenjin. All rights reserved.
//

#import "ZhangDanListTableViewCell.h"
#import "ZhangDanEntity+CoreDataClass.h"
#import "CoreDataManager.h"

@implementation ZhangDanListTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)reloadData:(id)data
{
    ZhangDanEntity* zdEntity = data;
    self.liRunLabel.text = zdEntity.lirun;
    self.zongJiaLabel.text = zdEntity.zongjia;
    self.chengBenLabel.text = zdEntity.chenben;
    
    //微转
    if ([zdEntity.payType integerValue] == 0) {
        
        self.payTypeLabel.text = @"微转";

        self.payTypeLabel.textColor = [UIColor colorWithRed:0 green:142 blue:87 alpha:1];

    }
    //打款
    else if ([zdEntity.payType integerValue] == 1)
    {
        self.payTypeLabel.text = @"打款";
        self.payTypeLabel.textColor = [UIColor colorWithRed:0 green:28 blue:84 alpha:1];

    }
    //现金
    else if ([zdEntity.payType integerValue] == 2)
    {
        self.payTypeLabel.text = @"现金";
        self.payTypeLabel.textColor = [UIColor colorWithRed:139 green:0 blue:98 alpha:1];

    }
    //欠款
    else if ([zdEntity.payType integerValue] == 3)
    {
        if ([zdEntity.isPay integerValue] == 0) {
            
            self.payTypeLabel.text = @"欠款";
            
            self.payTypeLabel.textColor = [UIColor redColor];

        }
        else
        {
            self.payTypeLabel.text = [NSString stringWithFormat:@"欠款（已还清）"];

        }
    }

    
    self.kehuLabel.text = zdEntity.kehuName;
    self.phoneLabel.text = zdEntity.kehuPhone;
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    //用[NSDate date]可以获取系统当前时间
    NSString *currentDateStr = [dateFormatter stringFromDate:zdEntity.time];
    self.timeLabel.text =  currentDateStr;

    self.shangPinInfoLabel.text = [ZhangDanListTableViewCell shangPinInfoWithEntity:zdEntity];
}

+(NSString*)shangPinInfoWithEntity:(ZhangDanEntity*)zdEntity
{
    NSMutableArray* entityArry = [[CoreDataManager shareManager] selectZhangDanShangPinRelationWithIds:zdEntity.zdsprIds];
    
    NSMutableArray* entityIdArry = [NSMutableArray array];
    
    for (ZhangDanShangPinRelationEntity* entity in entityArry) {
        
        [entityIdArry addObject:[NSString stringWithFormat:@"%lld",entity.spId]];
    }
    
    NSMutableArray<ShangPinEntity*>* spEntityArry = [[CoreDataManager shareManager] selectShangPinWithIds:[entityIdArry componentsJoinedByString:@","]];
    
    NSMutableArray* infoArry = [[NSMutableArray alloc] init];
    
    for (NSInteger i=0; i<spEntityArry.count; i++) {
        
        ShangPinEntity* tmpSPEntity = [spEntityArry objectAtIndex:i];

        for (ZhangDanShangPinRelationEntity* entity in entityArry) {
            
            if (entity.spId == tmpSPEntity.sId) {
                
                [infoArry addObject:[NSString stringWithFormat:@"%@    %lld件",tmpSPEntity.name,entity.shuliang]];
                break;
            }
        }
    }
    
    return [infoArry componentsJoinedByString:@"\n"];
}

@end
